package com.music.utils.file;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

public class ServerUtil extends FileUtil {
    @Value(value = "${application.file.path}")
    private String path;

    private static final ServerUtil serverUtil = new ServerUtil();

    public static ServerUtil getInstance() {
        return serverUtil;
    }

    @Override
    public String upload(MultipartFile file, String fileName) throws Exception {
        assert file != null;
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdir();
        }
        File newFile = new File(path, fileName);
        file.transferTo(newFile);
        return "/api/point/download/" + fileName;
    }

    @Override
    public void remove(String fileName, String type) throws Exception {
        if (fileName.contains("周杰伦"))
            return;
        File deleteFile = new File(path + type+ "/" +fileName);
        deleteFile.delete();
    }
}
